package com.example.lj.uav;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.example.lj.uav.dao.MyDBDao;
import com.example.lj.uav.utils.OnUIUtils;
import com.secneo.sdk.Helper;

import java.util.ArrayList;
import java.util.List;

import dji.common.camera.SystemState;
import dji.sdk.airlink.AirLink;
import dji.sdk.base.BaseProduct;
import dji.sdk.battery.Battery;
import dji.sdk.camera.Camera;
import dji.sdk.flightcontroller.FlightController;
import dji.sdk.flightcontroller.FlyZoneManager;
import dji.sdk.gimbal.Gimbal;
import dji.sdk.media.MediaFile;
import dji.sdk.products.Aircraft;
import dji.sdk.products.HandHeld;
import dji.sdk.remotecontroller.RemoteController;
import dji.sdk.sdkmanager.DJISDKManager;

public class MyApplication extends Application {

    private Context context;
    private static Application app = null;
    private static Handler mHandler;
    public static SharedPreferences mPref;
    public static MyDBDao dao;
    public static final String FLAG_CONNECTION_CHANGE = "dji_sdk_connection_change";
    //飞行器连接状态
    public static String accountName;
    private static BaseProduct mProduct;
    private static FlyZoneManager mFlyZone;
    //
    private static List<MediaFile> savedMediaFiles = new ArrayList<>();

    public static Application getInstance() {
        return MyApplication.app;
    }

    @Override
    protected void attachBaseContext(Context paramContext) {
        super.attachBaseContext(paramContext);
        mHandler = new Handler(Looper.getMainLooper());
        Helper.install(MyApplication.this);
        app = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        //  关联账户时再调用
//        mPref = getSharedPreferences("config", MODE_PRIVATE);
        dao = MyDBDao.getInstance(this);
    }

    public void notifyStatusChange() {
        mHandler.removeCallbacks(updateRunnable);
        mHandler.postDelayed(updateRunnable, 500);
    }

    private Runnable updateRunnable = new Runnable() {

        @Override
        public void run() {
            Intent intent = new Intent(FLAG_CONNECTION_CHANGE);
            sendBroadcast(intent);
        }
    };

    /**
     * This function is used to get the instance of DJIBaseProduct.
     * If no product is connected, it returns null.
     */
    public static synchronized BaseProduct getProductInstance() {
        if (null == mProduct) {
            mProduct = DJISDKManager.getInstance().getProduct();
        }
        return mProduct;
    }

    public static synchronized FlightController getFlightControllerInstance() {
        if (getProductInstance() == null)
            return null;

        FlightController fc = null;

        if (getProductInstance() instanceof Aircraft) {
            fc = ((Aircraft) getProductInstance()).getFlightController();
        }
        return fc;
    }

    public static synchronized AirLink getAirLinkInstance() {
        if (getProductInstance() == null)
            return null;

        AirLink al = null;

        if (getProductInstance() instanceof Aircraft) {
            al = ((Aircraft) getProductInstance()).getAirLink();
        }
        return al;
    }

    public static synchronized List<Battery> getBatteryInstance() {
        if (getProductInstance() == null)
            return null;

        List<Battery> batteryList = new ArrayList<>();

        if (getProductInstance() instanceof Aircraft) {
            List<Battery> b = ((Aircraft) getProductInstance()).getBatteries();
            if (b == null || b.size() <= 0) {
                if (getProductInstance().getBattery() != null)
                    batteryList.add(getProductInstance().getBattery());
            } else {
                batteryList = b;
            }
        }
        return batteryList;
    }

    public static synchronized Camera getCameraInstance() {
        if (getProductInstance() == null)
            return null;

        Camera camera = null;

        if (getProductInstance() instanceof Aircraft) {
            camera = ((Aircraft) getProductInstance()).getCamera();
        } else if (getProductInstance() instanceof HandHeld) {
            camera = ((HandHeld) getProductInstance()).getCamera();
        }
//        if (camera!=null){
//            camera.setSystemStateCallback(new SystemState.Callback() {
//                @Override
//                public void onUpdate(@NonNull SystemState systemState) {
//
//                }
//            });
//
//            camera.setMediaFileCallback(new MediaFile.Callback() {
//                @Override
//                public void onNewFile(@NonNull MediaFile mediaFile) {
//
//                }
//            });
//        }
        return camera;
    }

    public static synchronized FlyZoneManager getFlyZoneInstance() {
        if (null == mFlyZone) {
            mFlyZone = DJISDKManager.getInstance().getFlyZoneManager();
        }
        return mFlyZone;
    }

    public static synchronized RemoteController getRemoteControllerInstance() {
        if (getProductInstance() == null)
            return null;
        RemoteController remoteController = null;
        if (getProductInstance() instanceof Aircraft) {
            remoteController = ((Aircraft) getProductInstance()).getRemoteController();
        }
        return remoteController;
    }

    public static synchronized List<Gimbal> getGimbalInstance() {
        if (getProductInstance() == null)
            return null;

        List<Gimbal> gimbalList = new ArrayList<>();
        if (getProductInstance() instanceof Aircraft) {
            List<Gimbal> g = ((Aircraft) getProductInstance()).getGimbals();
            if (g == null || g.size() <= 0) {
                if (getProductInstance().getGimbal() != null)
                    gimbalList.add(getProductInstance().getGimbal());
            } else {
                gimbalList = g;
            }
        }
        return gimbalList;
    }

    public static void setSavedMediaFiles(List<MediaFile> list){
        savedMediaFiles = list;
    }

    public static List<MediaFile> getSavedMediaFiles(){
        return savedMediaFiles;
    }
}
